VER=0.9
SCRATCHBOX_HOME=/scratchbox/users/polster/home/polster
SCRATCHBOX_TMP=vpntool.tmp

SCRATCHBOX_WORK=$(SCRATCHBOX_HOME)/$(SCRATCHBOX_TMP)

all: dmg deb deb-maemo

clean:
	rm -rf dist build $(SCRATCHBOX_WORK)
	cd src; make clean

dmg:
	cd src; make
	rm -rf build dist/mac dist/*.dmg
	python pkg/setup.py py2app --includes sip
	sh pkg/setver $(VER) dist
	mkdir -p dist/mac
	mv dist/vpntool.app dist/mac
	hdiutil create dist/vpntool-$(VER)-leopard.dmg \
	  -volname "vpntool $(VER)" -fs HFS+ -srcfolder "dist/mac" -format UDBZ
	# hdiutil makehybrid -hfs -o dist/vpntool-$(VER)-leopard.dmg \
	#   -hfs-volume-name "vpntool $(VER)" dist/mac

deb: deb-base
	cp pkg/control dist/deb/DEBIAN/control
	chmod a+rx dist/deb/DEBIAN/control
	sh pkg/setver $(VER) dist
	fakeroot dpkg --build dist/deb dist/vpntool-$(VER)_all.deb

deb-maemo:
	if [ ! -d $(SCRATCHBOX_HOME) ]; then \
	  echo "Set SCRATCHBOX_HOME to your scratchbox home directory"; \
	  exit 1; \
	fi
	cd src; make
	cd $(SCRATCHBOX_HOME) && rm -rf $(SCRATCHBOX_TMP)
	mkdir $(SCRATCHBOX_WORK)
	cp -R * $(SCRATCHBOX_WORK)
	sh pkg/setver $(VER) $(SCRATCHBOX_WORK)
	(echo cd $(SCRATCHBOX_TMP); echo make deb-maemo-scratchbox) | \
	  scratchbox -s
	mkdir -p dist
	cp $(SCRATCHBOX_WORK)/dist/vpntool-$(VER)_all_maemo.deb dist
	
deb-maemo-scratchbox: deb-base
	cp pkg/control-maemo dist/deb/DEBIAN/control
	chmod a+rx dist/deb/DEBIAN/control
	mkdir dist/deb/usr/share/applications/hildon
	mv dist/deb/usr/share/applications/vpntool.desktop \
	  dist/deb/usr/share/applications/hildon
	chmod -R a+rx dist/deb/usr/share/applications/hildon
	mkdir -p dist/deb/etc/sudoers.d
	cp pkg/vpntool.sudoers dist/deb/etc/sudoers.d
	chmod -R ug+rx,o-rwx dist/deb/etc
	fakeroot dpkg --build dist/deb dist/vpntool-$(VER)_all_maemo.deb
	
deb-base:
	cd src; make
	rm -rf dist/deb
	mkdir -p \
          dist/deb/DEBIAN dist/deb/usr/bin dist/deb/usr/share/icons \
	  dist/deb/usr/share/applications dist/deb/usr/share/vpntool \
	  dist/deb/usr/share/doc/vpntool
	cp pkg/control pkg/postinst pkg/prerm dist/deb/DEBIAN
	cp pkg/vpntool.png dist/deb/usr/share/icons
	cp pkg/vpntool.desktop dist/deb/usr/share/applications
	cp pkg/copyright pkg/changelog dist/deb/usr/share/doc/vpntool
	cp src/*.py dist/deb/usr/share/vpntool
	mv dist/deb/usr/share/vpntool/vpntool.py \
	  dist/deb/usr/bin/vpntool
	chmod a+rx \
	  dist/deb/usr/bin/vpntool dist/deb/usr/share/vpntool \
	  dist/deb/usr/share/doc/vpntool dist/deb/DEBIAN dist/deb/DEBIAN/*
	chmod -R a+r dist/deb
	cd src; make clean
